package com.example.demo.dao.impl;

import com.example.demo.dao.BaseDaoImpl;
import com.example.demo.dao.IOrderDao;
import com.example.demo.model.po.ApplyPage;
import com.example.demo.model.po.OrderPage;
import com.example.demo.model.request.ApplyPageRequest;
import com.example.demo.model.request.OrderPageRequest;
import com.example.demo.util.AssertUtils;
import com.example.demo.util.DateUtils;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.SelectJoinStep;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.example.demo.jooq.tables.Account.ACCOUNT;
import static com.example.demo.jooq.tables.Apply.APPLY;
import static com.example.demo.jooq.tables.ShopOrder.SHOP_ORDER;
import static com.example.demo.jooq.tables.ThirdProvider.THIRD_PROVIDER;
import static org.jooq.impl.DSL.count;

@Component
public class IOrderDaoImpl extends BaseDaoImpl implements IOrderDao {

    @Override
    public List<OrderPage> queryPageOrderList(OrderPageRequest orderPageRequest) {
        //查询条件
        List<Condition> conditions = new ArrayList<>();
        if(AssertUtils.isNotEmpty(orderPageRequest.getKeyword())){
            conditions.add(APPLY.SHOP_NAME.likeRegex(orderPageRequest.getKeyword().trim())
                    .or(APPLY.PRODUCT_NAME.likeRegex(orderPageRequest.getKeyword().trim()))
                    .or(APPLY.PRODUCT_BELONG_USER.likeRegex(orderPageRequest.getKeyword().trim()))
                    .or(APPLY.ASSIN.likeRegex(orderPageRequest.getKeyword().trim()))
                    .or(SHOP_ORDER.ORDER_NO.likeRegex(orderPageRequest.getKeyword().trim()))
                    .or(SHOP_ORDER.OPTION_USER_NAME.likeRegex(orderPageRequest.getKeyword().trim()))
                    .or(ACCOUNT.EMAIL.likeRegex(orderPageRequest.getKeyword().trim()))
                    .or(THIRD_PROVIDER.PROVIDER_CONTACT_NAME.likeRegex(orderPageRequest.getKeyword().trim()))
            );
        }
        if(AssertUtils.isNotEmpty(orderPageRequest.getCreatedDate())){
            conditions.add(SHOP_ORDER.CREATED_DATE.eq(DateUtils.stringToTime(orderPageRequest.getCreatedDate())));
        }
        if(AssertUtils.isNotEmpty(orderPageRequest.getOrderStatus())){
            conditions.add(SHOP_ORDER.ORDER_STATUS.eq(orderPageRequest.getOrderStatus()));
        }
        if(AssertUtils.isNotEmpty(orderPageRequest.getSite())){
            conditions.add(APPLY.SITE.eq(orderPageRequest.getSite()));
        }
        if(AssertUtils.isNotEmpty(orderPageRequest.getDueCommentDate())){
            conditions.add(SHOP_ORDER.DUE_COMMENT_DATE.eq(DateUtils.stringToTime(orderPageRequest.getDueCommentDate())));
        }
        if(AssertUtils.isNotEmpty(orderPageRequest.getAccountSource())){
            conditions.add(SHOP_ORDER.ACCOUNT_SOURCE.eq(orderPageRequest.getAccountSource()));
        }
        if(AssertUtils.isNotEmpty(orderPageRequest.getApplySource())){
            conditions.add(APPLY.APPLY_SOURCE.eq(orderPageRequest.getApplySource()));
        }
        if(AssertUtils.isNotEmpty(orderPageRequest.getBusinessType())){
            conditions.add(APPLY.BUSINESS_TYPE.eq(orderPageRequest.getBusinessType()));
        }
        SelectJoinStep<Record> totalLine = this.getDslContext()
                .select(APPLY.fields())
                .select(SHOP_ORDER.fields())
                .select(ACCOUNT.ACCOUNT_ID,ACCOUNT.EMAIL)
                .select(THIRD_PROVIDER.PROVIDER_COMPANY_NAME,THIRD_PROVIDER.THIRD_PROVIDER_ID,
                        THIRD_PROVIDER.PROVIDER_CONTACT_NAME,
                        THIRD_PROVIDER.PROVIDER_CONTACT_METHOD,
                        THIRD_PROVIDER.PROVIDER_CONTACT_NO,
                        THIRD_PROVIDER.PROVIDER_COMMISSION_AMOUNT,
                        THIRD_PROVIDER.PROVIDER_EXCHANGE_RATE,
                        THIRD_PROVIDER.PROVIDER_REFUND_METHOD,
                        THIRD_PROVIDER.PROVIDER_REFUND_ACCOUNT_NO,
                        THIRD_PROVIDER.PROVIDER_REMARK)
                .select(count().over().as("totalLine"))
                .from(SHOP_ORDER)
                .leftJoin(ACCOUNT).on(SHOP_ORDER.ACCOUNT_ID.eq(ACCOUNT.ACCOUNT_ID))
                .leftJoin(APPLY).on(APPLY.APPLY_ID.eq(SHOP_ORDER.APPLY_ID))
                .leftJoin(THIRD_PROVIDER).on(THIRD_PROVIDER.THIRD_PROVIDER_ID.eq(SHOP_ORDER.THIRD_PROVIDER_ID));
        totalLine.where(conditions);
        return totalLine.orderBy(SHOP_ORDER.CREATED_DATE.desc())
                .offset(orderPageRequest.getOffset())
                .limit(orderPageRequest.getPageSize())
                .fetchInto(OrderPage.class);
    }

    @Override
    public List<ApplyPage> queryPageApplyList(ApplyPageRequest applyPageRequest) {
        //查询条件
        List<Condition> conditions = new ArrayList<>();
        if(AssertUtils.isNotEmpty(applyPageRequest.getKeyword())){
            conditions.add(APPLY.PRODUCT_NAME.likeRegex(applyPageRequest.getKeyword().trim())
                    .or(APPLY.SHOP_NAME.likeRegex(applyPageRequest.getKeyword().trim()))
                    .or(APPLY.APPLY_ID.likeRegex(applyPageRequest.getKeyword().trim()))
                    .or(APPLY.ASSIN.likeRegex(applyPageRequest.getKeyword().trim()))
                    .or(APPLY.SEARCH_KEYWORD.likeRegex(applyPageRequest.getKeyword().trim()))
                    .or(APPLY.PRODUCT_BELONG_USER.likeRegex(applyPageRequest.getKeyword().trim()))
                    .or(APPLY.OPTION_USER_NAME.likeRegex(applyPageRequest.getKeyword().trim()))
            );
        }
        if(AssertUtils.isNotEmpty(applyPageRequest.getApplyDate())){
            conditions.add(APPLY.APPLY_DATE.eq(DateUtils.stringToTime(applyPageRequest.getCreatedDate())));
        }
        if(AssertUtils.isNotEmpty(applyPageRequest.getCreatedDate())){
            conditions.add(APPLY.CREATED_DATE.eq(DateUtils.stringToTime(applyPageRequest.getCreatedDate())));
        }
        if(AssertUtils.isNotEmpty(applyPageRequest.getApplySource())){
            conditions.add(APPLY.APPLY_SOURCE.likeRegex(applyPageRequest.getApplySource()));
        }
        if(AssertUtils.isNotEmpty(applyPageRequest.getApplyStatus())){
            conditions.add(APPLY.APPLY_STATUS.eq(applyPageRequest.getApplyStatus()));
        }
        if(AssertUtils.isNotEmpty(applyPageRequest.getBusinessType())){
            conditions.add(APPLY.BUSINESS_TYPE.eq(applyPageRequest.getBusinessType()));
        }
        if(AssertUtils.isNotEmpty(applyPageRequest.getSite())){
            conditions.add(APPLY.SITE.eq(applyPageRequest.getSite()));
        }
        SelectJoinStep<Record> totalLine = this.getDslContext()
                .select(APPLY.fields())
                .select(count().over().as("totalLine"))
                .from(APPLY);
        totalLine.where(conditions);
        return totalLine.orderBy(APPLY.CREATED_DATE.desc())
                .offset(applyPageRequest.getOffset())
                .limit(applyPageRequest.getPageSize())
                .fetchInto(ApplyPage.class);
    }


    @Override
    public OrderPage queryOrderDetail(String orderId) {
        return this.getDslContext()
                .select(APPLY.fields())
                .select(SHOP_ORDER.fields())
                .select(ACCOUNT.ACCOUNT_ID,ACCOUNT.EMAIL)
                .select(THIRD_PROVIDER.PROVIDER_COMPANY_NAME,THIRD_PROVIDER.THIRD_PROVIDER_ID,
                        THIRD_PROVIDER.PROVIDER_CONTACT_NAME,
                        THIRD_PROVIDER.PROVIDER_CONTACT_METHOD,
                        THIRD_PROVIDER.PROVIDER_CONTACT_NO,
                        THIRD_PROVIDER.PROVIDER_COMMISSION_AMOUNT,
                        THIRD_PROVIDER.PROVIDER_EXCHANGE_RATE,
                        THIRD_PROVIDER.PROVIDER_REFUND_METHOD,
                        THIRD_PROVIDER.PROVIDER_REFUND_ACCOUNT_NO,
                        THIRD_PROVIDER.PROVIDER_REMARK)
                .from(SHOP_ORDER)
                .leftJoin(ACCOUNT).on(SHOP_ORDER.ACCOUNT_ID.eq(ACCOUNT.ACCOUNT_ID))
                .leftJoin(APPLY).on(APPLY.APPLY_ID.eq(SHOP_ORDER.APPLY_ID))
                .leftJoin(THIRD_PROVIDER).on(THIRD_PROVIDER.THIRD_PROVIDER_ID.eq(SHOP_ORDER.THIRD_PROVIDER_ID))
                .where(SHOP_ORDER.ORDER_ID.eq(orderId))
                .fetchOneInto(OrderPage.class);
    }

    @Override
    public List<OrderPage> queryOrderListByIds(List<String> orderIds) {
        return this.getDslContext()
                .select(APPLY.fields())
                .select(SHOP_ORDER.fields())
                .select(ACCOUNT.ACCOUNT_ID,ACCOUNT.EMAIL)
                .from(SHOP_ORDER)
                .leftJoin(ACCOUNT).on(SHOP_ORDER.ACCOUNT_ID.eq(ACCOUNT.ACCOUNT_ID))
                .leftJoin(APPLY).on(APPLY.APPLY_ID.eq(SHOP_ORDER.APPLY_ID))
                .where(SHOP_ORDER.ORDER_ID.in(orderIds)).orderBy(SHOP_ORDER.CREATED_DATE.desc())
                .fetchInto(OrderPage.class);
    }
}
